package mvp.com.register.view.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import mvp.com.register.R;
import mvp.com.register.utils.LogUtils;

/**
 * 首页带文字的自定义View
 * Created by Administrator on 2016/9/17 0017.
 */
public class EndImageWithText extends MyCustomView {

    private static final String TAG = "EndImageWithText";

    private ImageView image;
    private TextView tv, tv_buy_number;
    private String title;
    private int src_one, src_two;
    private int strSize;
    private EndImageListener listener;

    /**
     * 突变点击监听
     */
    public interface EndImageListener {
        public void imageClick(int id);
    }

    public EndImageWithText(Context context) {
        super(context);
    }

    public EndImageWithText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EndImageWithText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setAttrs(AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EndImageWithText);
        title = typedArray.getString(R.styleable.EndImageWithText_text);
        src_one = typedArray.getResourceId(R.styleable.EndImageWithText_bground_one, -1);
        src_two = typedArray.getResourceId(R.styleable.EndImageWithText_bground_two, -1);
        strSize = typedArray.getInt(R.styleable.EndImageWithText_strsize, 12);
    }

    @Override
    public void initView(LayoutInflater inflater) {
        inflater.inflate(R.layout.end_image_with_text_view, this, true);
        image = (ImageView) findViewById(R.id.imageWithText_button);
        tv = (TextView) findViewById(R.id.imageWithText_text);
        tv_buy_number = (TextView) findViewById(R.id.tv_buy_number);

        tv.setText(title);
        tv.setTextSize(strSize);
        image.setImageResource(src_one);
        tv_buy_number.setVisibility(View.GONE);

        image.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                LogUtils.i(TAG, "Button_click");
                if (listener != null) {
                    listener.imageClick(EndImageWithText.this.getId());
                }
            }
        });
    }

    /**
     * 改变控件状态
     *
     * @param status
     */
    public void changeImageWithTextStatus(boolean status) {
        if (status) {
            tv.setTextColor(getResources().getColor(R.color.main_blue));
            image.setImageResource(src_one);
            //  Drawable icon=context.getResources().getDrawable(src_one);
            //  image.setDrawableIcon(icon);

        } else {
            tv.setTextColor(getResources().getColor(R.color.main_grade));
            image.setImageResource(src_two);
            //  Drawable icon=context.getResources().getDrawable(src_two);
            //  image.setDrawableIcon(icon);
        }
    }

    public void setBuyNumber(boolean isShow, int number) {
        if (isShow) {
            tv_buy_number.setVisibility(View.VISIBLE);
            if (number != 0) {
                tv_buy_number.setText(number + "");
            } else {
                tv_buy_number.setText("");
            }
        } else {
            tv_buy_number.setVisibility(View.GONE);
        }
    }
}
